const apiMethods = {
  methods: {
    apiGet(url, data) {
    	let that = this;
      return new Promise((resolve, reject) => {
        axios.get(url, data).then((response) => {
        	if(response.data.code == 200) {
        		resolve(response.data)
        	}else if(response.data.code == 1009) {
						bus.$message({
							message: response.data.err,
							type: 'error'
						})
						that.goLoginPage()
					} else {
        		that.$message({
        			type: 'warning',
        			message: response.data.err
        		})
        	}
        }, (response) => {
          _g.closeGlobalLoading()
        	bus.$message({
            message: '捕获异常',
            type: 'warning'
          })
        	that.goLoginPage()
          reject(response)
        })
      })
    },
    apiPost(url, data) {
    	let that = this;
      return new Promise((resolve, reject) => {
        axios.post(url, data).then((response) => {
        	if(response.data.code == 200) {
        		resolve(response.data)
        	}else if(response.data.code == 1009) {
						bus.$message({
							message: response.data.err,
							type: 'error'
						})
						that.goLoginPage()
					} else {
        		that.$message({
        			type: 'warning',
        			message: response.data.err
        		})
        	}
        }).catch((response) => {
          _g.closeGlobalLoading()
          bus.$message({
            message: '捕获异常',
            type: 'warning'
          })
        	that.goLoginPage()
          resolve(response)
        })
      })
    },
//  apiPost(url, data) {
//  	let that = this;
//    return new Promise((resolve, reject) => {
//      axios.post(url, data).then((response) => {
//        resolve(response.data)
//      }).catch((response) => {
//        _g.closeGlobalLoading()
//        bus.$message({
//          message: '捕获异常',
//          type: 'warning'
//        })
//      	that.goLoginPage()
//        resolve(response)
//      })
//    })
//  },
    apiDelete(url, id) {
      return new Promise((resolve, reject) => {
        axios.delete(url + id).then((response) => {
          resolve(response.data)
        }, (response) => {
          _g.closeGlobalLoading()
					bus.$message({
            message: '捕获异常',
            type: 'warning'
          })
					reject(response)
        })
      })
    },
    apiPut(url, id, obj) {
      return new Promise((resolve, reject) => {
        axios.put(url + id, obj).then((response) => {
          resolve(response.data)
        }, (response) => {
          _g.closeGlobalLoading()
          bus.$message({
            message: '请求超时，请检查网络',
            type: 'warning'
          })
          reject(response)
        })
      })
    },
    goLoginPage() {
    	this.$router.push({path: '/login'})
    }
    
  },
  computed: {
    showLoading() {
      return store.state.globalLoading
    }
  }
}

export default apiMethods
